package org.example.server.pop;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.enums.TaskEnum;
import org.example.po.AttributeConfigParseDto;
import org.example.po.PopCorpClientManagerDto;
import org.example.po.TaskDto;
import org.example.utils.MD5Util;
import org.example.utils.http.RedirectUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.example.server.pop.ToPop.popLogin;

@Slf4j
public class TaskConfig {
    public static void openTask(String phone, String password) {
        TaskEnum[] values = TaskEnum.values();
        for (TaskEnum task : values) {
            PopCorpClientManagerDto popCorpClientManagerDto = popLogin(phone, password);
            TaskDto taskDto = new TaskDto();
            List<AttributeConfigParseDto> attributeConfigParseDtos = JSON.parseArray(task.getAttMapper(), AttributeConfigParseDto.class);
            taskDto.setAttributeConfigParseDto(attributeConfigParseDtos);
            taskDto.setOrgId(popCorpClientManagerDto.getOrgId());
            taskDto.setTaskType(taskDto.getTaskType());
            taskDto.setAppId(popCorpClientManagerDto.getAppId());
            taskDto.setInterval(task.getInterval());
            taskDto.setTaskInfo(task.getTaskInfo());
            taskDto.setTaskInfoDemo(task.getTaskInfo());
            taskDto.setTaskId(task.getTaskId());
            taskDto.setStatus((byte) 1);
            String taskDtoStr = JSON.toJSONString(taskDto);
            log.info("保存任务信息taskDtoStr：{}", taskDtoStr);
            String singTaskDtoStr = MD5Util.encrypt(taskDtoStr + popCorpClientManagerDto.getAppSecret());
            log.info("保存任务信息singTaskDtoStr：" + singTaskDtoStr + "appSecret:" + popCorpClientManagerDto.getAppSecret());
            Map<String, String> parmsMap = new HashMap<>();
            parmsMap.put("taskDtoStr", taskDtoStr);
            parmsMap.put("singTaskDtoStr", singTaskDtoStr);
            String result = RedirectUtil.doPostByFrom("https://h-api.ybm100.com/api/hack/pop/saveorupdatetask", parmsMap);
            log.info("请求入参-> url:{} body: {}", "https://h-api.ybm100.com/api/hack/pop/saveorupdatetask", JSON.toJSONString(parmsMap));
            log.info("任务开启反馈信息-->{}", result);
        }

    }
}
